package com.zhb.cloud.plant.active.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.zhb.cloud.base.BaseController;
import com.zhb.cloud.core.JsonReturn;
import com.zhb.cloud.plant.active.model.Prize;
import com.zhb.cloud.plant.active.model.User;
import com.zhb.cloud.plant.active.service.UserService;
import com.zhb.cloud.utils.EncryptionUtils;
import com.zhb.cloud.utils.UUIDUtil;

@RestController
@RequestMapping("user")
public class UserController extends BaseController {
	@Autowired
	private UserService userService;

	/**
	 * @Title: toAward
	 * @author: zhb
	 * @date: 2017年8月1日 上午11:35:44
	 * @Description: 跳转到抽奖页面
	 * @return
	 */
	@GetMapping("toAward")
	public ModelAndView toAward() {
		return new ModelAndView("index");
	}

	/**
	 * @Description: 用户抽奖
	 * @author: zhb zhb1316783262@163.com
	 * @date: 2017年11月20日 上午10:18:11
	 * @return
	 */
	@PostMapping("lottery")
	public JsonReturn lottery(String userId) {
		JsonReturn jsonReturn = new JsonReturn();
		Prize prize = userService.lottery(userId);
		jsonReturn.setCode(200);
		jsonReturn.setData(prize);
		jsonReturn.setMessage("查询成功");
		return jsonReturn;
	}

	@PostMapping("testLottery")
	public JsonReturn testLottery() {
		ExecutorService threadPool = Executors.newFixedThreadPool(10);
		List<User> users = userService.selectModelByCondition();
		List<Callable<Prize>> callables = new ArrayList<>();
		for (User user : users) {
			Callable<Prize> callable = new Callable<Prize>() {
				@Override
				public Prize call() {
					Prize lottery = userService.lottery(user.getId());
					System.out.println(lottery.getPrizeName());
					return lottery;
				}
			};
			callables.add(callable);
		}
		for (Callable<Prize> callable : callables) {
			try {
				threadPool.submit(callable);
			} catch (Exception e) {
				threadPool.shutdown();
				e.printStackTrace();
			}
		}
		return this.success("sdas");
	}

	/**
	 * @Title: init
	 * @author: zhb
	 * @date: 2017年11月26日 上午10:03:42
	 * @Description: 初始化用户
	 * @return
	 */
	@PostMapping("init")
	public JsonReturn init() {
		List<User> list = initUser();
		userService.batchInsert(list);
		return this.success(list);
	}

	/**
	 * @Description: 用户抽奖
	 * @author: zhb zhb1316783262@163.com
	 * @date: 2017年11月20日 上午10:18:11
	 * @return
	 */

	/**
	 * @Title: initUser
	 * @author: zhb
	 * @date: 2017年11月26日 上午9:55:13
	 * @Description: 初始化用户
	 * @return
	 */
	protected List<User> initUser() {
		List<User> users = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			User user = new User();
			user.setId(UUIDUtil.getId());
			user.setEmail("131678326" + i + "qq.com");
			user.setPhone("1331987092" + i);
			user.setGender(i % 2 == 0 ? 1 : 0);
			user.setAddress("长安街" + i + "号");
			user.setRealName("张" + i + "元");
			user.setRegisterTime(new Date());
			user.setPassword(EncryptionUtils.getEncryptionPassword("123"));
			user.setUserName("李" + i + "大");
			users.add(user);
		}
		return users;
	}
}
